<?php
/**
 * Description: 微信打赏课程列表
 * User: devforma
 * Date: 16/7/27
 * Time: 13:49
 */

require LIB_DIR . '/LiveCourse.class.php';
class WeixinRewardCourses implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1) return genOutput(1001);

        //获取配置好的微信打赏课程
        $coursIds = $cmsdb->getVar('SELECT `option_value` FROM `wp_options` WHERE `option_name`=:opt', [':opt' => 'weixinReward']);

        if (empty($coursIds))
            return genOutput(1001);

        //获取已购课程
        $purchased = $this->getPurchasedCourses($queryArgs['user_id']);

        $courses = $cmsdb->getResults("SELECT * FROM `data_live_course` WHERE `id` IN ({$coursIds}) ORDER BY FIELD (`id`, {$coursIds})");
        $list = [];
        foreach ($courses as $c) {
            $computedCourseInfo = LiveCourse::getComputedCourseInfo($c->id);
            $list[] = [
                'id' => (int)$c->id,
                'name' => $c->course_name,
                'intro' => $c->introduction,
                'start_time' => $this->calShowStartTime($c->class_starttime, $c->course_desc),
                'end_time' => $this->calShowEndTime($c->class_endtime, $c->course_desc),
                'price' => (float)$c->price,
                'lectors' => array_values($computedCourseInfo['lectors']),
                'need_address' => (bool)$c->need_address,
                'need_qq' => (bool)$c->need_qq,
                'is_purchased' => in_array($c->id, $purchased)
            ];
        }

        return genOutput(1, [
            'courses' => $list
        ]);
    }

    /**
     * 获取用户购买的课程id
     * @param int $userId 用户id
     * @return array
     */
    private function getPurchasedCourses($userId) {
        global $cmsdb;

        $buyed = (array)$cmsdb->getCol('SELECT `product_id`FROM `relation_user_product` WHERE `user_id`=:uid AND `product_type`="zhiboke"',
            array(':uid' => $userId));

        return $buyed;
    }


    /**
     * 计算供显示的开始时间(由课堂开始时间和手填的课程时间说明(格式:2015.01.10[-2015.04.12])共同决定)
     *
     * @param string $classStartTime 课堂开始时间
     * @param string $courseDateDesc 手填的课程时间说明
     * @return mixed
     */
    private function calShowStartTime($classStartTime, $courseDateDesc) {
        $times = explode('-', $courseDateDesc);
        $times[0] = str_replace('.', '-', $times[0]);

        $t = strtotime($times[0]);

        return $t == false ? $classStartTime : date('Y-m-d H:i:s', $t);
    }

    /**
     * 计算供显示的结束时间(由课堂开始时间和手填的课程时间说明(格式:2015.01.10[-2015.04.12])共同决定)
     *
     * @param string $classEndTime 课堂结束时间
     * @param string $courseDateDesc 手填的课程时间说明
     * @return mixed
     */
    private function calShowEndTime($classEndTime, $courseDateDesc) {
        $times = explode('-', $courseDateDesc);
        if (!isset($times[1]))
            return $classEndTime;

        $times[1] = str_replace('.', '-', $times[1]);
        $t = strtotime($times[1]);

        return $t == false ? $classEndTime : date('Y-m-d H:i:s', $t);
    }
}